<?php
/**
 * Copyright (c) 2006-2007, Julien PORTALIER
 * 
 * Licensed under The LGPL License
 * Redistributions of files must retain the above copyright notice.
 * 
 * @copyright    Copyright (c) 2006-2007, Julien PORTALIER
 * @link         http://feather-cm.googlecode.com/
 * @license      http://www.opensource.org/licenses/lgpl-license.php The LGPL License
 */

class InvitationsController extends AppController
{
	public $restrictActions = array('send', 'refill');
	
	function send()
	{
		#. validation
		if (empty($this->data['Invitation']['email']))
		{
			if (empty($_GET['email'])) {
				$this->flash(_('Please enter an email.'), '/my', 'error');
			}
			$this->data['Invitation']['email'] = $_GET['email'];
		}
		
		#. can invite?
		$Member = $this->load->model('Member');
		if (!$Member->has_invitations_left($this->Auth->id)) {
			$this->flash(_('No more invitations left.'), HTTP::getReferer(), 'error', 403);
		}
		
		#. generate code
		if ($this->Invitation->create($this->Auth->id))
		{
			#. deincrement counter
			$Member->decrement_invitations_counter($this->Auth->id);
		}
		else {
			$this->flash(_('An error occured.'), HTTP::getReferer(), 'error', 500);
		}
		
		#. send it
		$this->load->component('Swift');
		$this->Swift->addRecipient($this->data['Invitation']['email']);
		$this->Swift->send(sprintf(_('Invitation from %s'), $this->Auth->id),
			array($this->Config->website['email'], $this->Config->website['name']));
		
		$this->flash(_("Invitation sent."), HTTP::getReferer(), 'notice');
	}
	
#	function refill()
#	{
#		$data  = array('invitations' => $this->params['form']['nb_invitations']);
#		$where = array('created', '< '.Time::SQL('-'.$this->params['form']['nb_months']).' months');
#		Database::get()->update('members', $where, $data);
#	}
}
?>
